package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.entity.BaseBgtEco;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.open.capacity.reimburse.entity.BasePayItemBgtEco;
import com.open.capacity.reimburse.dao.BasePayItemBgtEcoDao;
import com.open.capacity.reimburse.service.BasePayItemBgtEcoService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class BasePayItemBgtEcoServiceImpl implements BasePayItemBgtEcoService {

    @Autowired
    private BasePayItemBgtEcoDao basePayItemBgtEcoDao;

    /**
     * 添加
     *
     * @param basePayItemBgtEcos
     */
    @Transactional
    public int save(List<BasePayItemBgtEco> basePayItemBgtEcos) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();


        for (BasePayItemBgtEco item : basePayItemBgtEcos) {
            int exists = basePayItemBgtEcoDao.exists(item.getPayitemId(), item.getBgtecoId());
            if (exists == 0) {
                item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                item.setUpdateUserid(loginUser.getId());
                item.setUpdateUsername(loginUser.getRealName());
                item.setUpdateTime(new Date());
                basePayItemBgtEcoDao.save(item);
            }
        }
        return 1;
    }

    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return basePayItemBgtEcoDao.delete(id);
    }


    @Override
    public List<BaseBgtEco> findByPayitemId(String payitemId) {
        return basePayItemBgtEcoDao.findByPayitemId(payitemId);
    }
}
